import request from '@/network'
import { Article } from './types'

// 上传文章配图
export const uploadPicturesRequest = async (formdata: FormData) => {
  const result = await request.post({
    url: '/upload/picture',
    data: formdata,
    headers: { 'Content-Type': 'multipart/form-data' },
  })
  return result
}
// 上传文章封面
export const uploadCoverRequest = async (formdata: FormData, article_id: number) => {
  const result = await request.post({
    url: '/upload/article_cover',
    data: formdata,
    params: {
      article_id,
    },
    headers: { 'Content-Type': 'multipart/form-data' },
  })
  return result
}
// 上传文章
export const uploadArticleRequest = async (mdContent: string, article_info: Article) => {
  const result = await request.post({
    url: '/article/article_info',
    data: {
      mdContent,
      ...article_info,
    },
  })
  return result
}

// 获取文章列表
export const queryArticleListRequest = async (currentPage: number = 1, pageSize: number = 7) => {
  const result = await request.get({
    url: '/article/article_list',
    params: {
      currentPage,
      pageSize,
    },
  })
  return result
}
// id获取文章内容
export const queryArticleByIdRequest = async (id: number) => {
  const result = await request.get({
    url: `/article/${id}/content`,
  })
  return result
}
// 修改文章置顶
export const updateStickRequest = async (article_id: number, is_sticky: boolean) => {
  const result = await request.patch({
    url: '/article/update_sticky',
    data: {
      article_id,
      is_sticky,
    },
  })
  return result
}
// 修改文章推荐
export const updateRecommendRequest = async (article_id: number, is_recommend: boolean) => {
  const result = await request.patch({
    url: '/article/update_recommend',
    data: {
      article_id,
      is_recommend,
    },
  })
  return result
}
// 修改文章内容
export const updateArticleContentRequest = async (article_id: number, edit_info: {}) => {
  const result = await request.patch({
    url: '/article/update_article_content',
    data: {
      article_id,
      edit_info,
    },
  })
  return result
}
// 删除文章
export const deleteArticleRequest = async (article_id: number) => {
  const result = await request.delete({
    url: '/article/delete_article',
    data: {
      article_id,
    },
  })
  return result
}
// 搜索
export const searchArticleRequest = async (searchInfo: { [key: string]: any }, currentPage: number = 1, pageSize: number = 7) => {
  const result = await request.post({
    url: '/article/search',
    data: {
      searchInfo,
      currentPage,
      pageSize,
    },
  })
  return result
}
